﻿// -----------------------------------------------------------------------
// <copyright file="ChessboardEstimator.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace JumpJump.AI
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using JumpJump.Runtime;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class StartDomain
    {
        #region Fields

        private Chessboard _currentBoard;

        private const double Coeffcient_MoveDepth = 1.5;

        private const double Coeffcient_MoveCount = 1.0;

        #endregion

        #region Properties


        #endregion

        #region Constructor

        public StartDomain(Chessboard board)
        {
            this._currentBoard = board;
        }

        #endregion

        #region Public Methods

        public int Estimate()
        {
            Initinal();
            _currentBoard.GetAllPlayersAllValidMovement();

            int value = 0;
            value += GetPrickPiecesMovementValue() * 5;
            value += GetPrickPiecesCorssValue();
            value -= GetEnemyPieceMovementValue();
            value -= GetEnemyPiecesCorssValue();

            return value;
        }

        public static Piece GetStandAlonePiece(Chessboard board)
        {
            List<Piece> lastPieces = GetLastPieces(board);

            if (lastPieces.Count == 0)
            {
                return null;
            }

            foreach (Piece piece in lastPieces)
            {
                if (IsStandAlonePiece(piece))
                {
                    return piece;
                }
            }

            return null;
        }

        #endregion 

        #region Private Methods

        private int GetPrickPiecesMovementValue()
        {
            int sum = 0;
            foreach (Piece onePiece in _currentBoard.PrickPieces)
            {
                sum += GetMovementValue(onePiece);
            }

            return sum;
        }

        private int GetPrickPiecesCorssValue()
        {
            int sum = 0;

            foreach (Piece onePiece in _currentBoard.PrickPieces)
            {
                sum += onePiece.DependencyCoordinateList.Count;
            }

            return sum;
        }

        private int GetEnemyPieceMovementValue()
        {
            int sum = 0;
            foreach (Piece onePiece in _currentBoard.UserPieces)
            {
                sum += GetMovementValue(onePiece);
            }

            return sum;
        }

        private int GetEnemyPiecesCorssValue()
        {
            int sum = 0;

            foreach (Piece onePiece in _currentBoard.UserPieces)
            {
                sum += onePiece.DependencyCoordinateList.Count;
            }

            return sum;
        }

        private int GetMovementValue(Piece currentPiece)
        {
            int maxDepth = -1;

            foreach (Coordinate coor in currentPiece.CanMoveToPositionList)
            {
                int depth = Math.Abs(coor.Y - currentPiece.Position.Y);
                if (depth > maxDepth)
                {
                    maxDepth = depth;
                }
            }

            return maxDepth * currentPiece.CanMoveToPositionList.Count;
        }

        private void GetAllMovement()
        {
            foreach (Piece onePiece in _currentBoard.UserPieces)
            {
                onePiece.GetNextPositions(_currentBoard.Board);
            }

            foreach (Piece onePiece in _currentBoard.PrickPieces)
            {
                onePiece.GetNextPositions(_currentBoard.Board);
            }
        }

        private void Initinal()
        {
            foreach (Piece piece in this._currentBoard.PrickPieces)
            {
                piece.CanMoveToPositionList.Clear();
                piece.DependencyCoordinateList.Clear();
            }

            foreach (Piece piece in this._currentBoard.UserPieces)
            {
                piece.CanMoveToPositionList.Clear();
                piece.DependencyCoordinateList.Clear();
            }
        }

        private static bool IsStandAlonePiece(Piece piece)
        {
            bool alone = true;

            if (piece.CanMoveToPositionList.Count == 0)
            {
                return false;
            }

            foreach (Coordinate coor in piece.CanMoveToPositionList)
            {
                if (coor.Y - piece.Position.Y != -1)
                {
                    alone = false;
                    break;
                }
            }
            return alone;
        }

        private static List<Piece> GetLastPieces(Chessboard board)
        {
            int yLast = -1;
            List<Piece> lastPieces = new List<Piece>();
            foreach (Piece p in board.PrickPieces)
            {
                if (p.Position.Y > yLast)
                {
                    yLast = p.Position.Y;
                }
            }

            foreach (Piece p in board.PrickPieces)
            {
                if (p.Position.Y == yLast)
                {
                    lastPieces.Add(p);
                }
            }

            return lastPieces;
        }

        #endregion
    }
}
